﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using myBoard.App.MagnetStore;
using System.Collections.ObjectModel;
using myBoard.App.Model;
using myBoard.Framework;

namespace myBoard.App.Components.MagnetsStore.ViewsModels
{
    public class MagnetsViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<MagnetViewModel> _connectedUserSubmittedMagnets;
        private ObservableCollection<MagnetViewModel> _activeCategoryMagnets;
        private ObservableCollection<MagnetViewModel> _latestMagnets;
        private MagnetViewModel _featuredMagnet;
        private Magnet _currentlyEditingMagnet;
        private MagnetViewModel _selectedMagnet;

        public MagnetViewModel SelectedMagnet
        {
            get { return _selectedMagnet; }
            set 
            {
                _selectedMagnet = value;
                RaisePropertyChanged("SelectedMagnet");
            }
        }

        public Magnet CurrentlyEditingMagnet
        {
            get { return _currentlyEditingMagnet; }
            set { _currentlyEditingMagnet = value; }
        }

        public ObservableCollection<MagnetViewModel> ActiveCategoryMagnets
        {
            get { return _activeCategoryMagnets; }
            set 
            {
                _activeCategoryMagnets = value;
                RaisePropertyChanged("ActiveCategoryMagnets");
            }
        }

        public MagnetViewModel FeaturedMagnet
        {
            get 
            {                
                return _featuredMagnet; 
            }
        }       

        public ObservableCollection<MagnetViewModel> ConnectedUserSubmittedMagnets
        {
            get 
            {
                return _connectedUserSubmittedMagnets; 
            }
        }

        public ObservableCollection<MagnetViewModel> LatestMagnets
        {
            get 
            {
                return _latestMagnets; 
            }            
        }

        public void UpdateLatestMagnetsAsync()
        {
            MagnetsStoreClient.Client.GetLatestMagnetsCompleted -= new EventHandler<GetLatestMagnetsCompletedEventArgs>(Client_GetLatestMagnetsCompleted);
            MagnetsStoreClient.Client.GetLatestMagnetsCompleted += new EventHandler<GetLatestMagnetsCompletedEventArgs>(Client_GetLatestMagnetsCompleted);
            MagnetsStoreClient.Client.GetLatestMagnetsAsync(InstancesSingleton.Instances.UsersViewModel.ConnectedUserSessionId);
        }

        void Client_GetLatestMagnetsCompleted(object sender, GetLatestMagnetsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnet Store","Failed to refresh latests magnets list", e.Error,System.Diagnostics.EventLogEntryType.Error, true);
                return;
            }
            _latestMagnets = new ObservableCollection<MagnetViewModel>();
            foreach(Magnet magnet in e.Result)
            {
                _latestMagnets.Add(new MagnetViewModel(magnet));
            }
            RaisePropertyChanged("LatestMagnets");
        }

        public MagnetsViewModel()
        {
            _activeCategoryMagnets = new ObservableCollection<MagnetViewModel>();
        }

        public void RefreshConnectedUserSubmittedMagnetsAsync()
        {
            MagnetsStoreClient.Client.GetUserSubmittedMagnetsCompleted += new EventHandler<GetUserSubmittedMagnetsCompletedEventArgs>(Client_GetUserSubmittedMagnetsCompleted);
            MagnetsStoreClient.Client.GetUserSubmittedMagnetsAsync(InstancesSingleton.Instances.UsersViewModel.ConnectedUserSessionId,
                InstancesSingleton.Instances.UsersViewModel.ConnectedUser.Id);
        }

        void Client_GetUserSubmittedMagnetsCompleted(object sender, GetUserSubmittedMagnetsCompletedEventArgs e)
        {
            ObservableCollection<Magnet> magnets = e.Result;
            _connectedUserSubmittedMagnets = new ObservableCollection<MagnetViewModel>();

            foreach (Magnet m in magnets)
                _connectedUserSubmittedMagnets.Add(new MagnetViewModel(m));

            RaisePropertyChanged("ConnectedUserSubmittedMagnets");
        }

        public void RefreshFeaturedMagnetAsync()
        {
            MagnetsStoreClient.Client.GetFeaturedMagnetCompleted += new EventHandler<GetFeaturedMagnetCompletedEventArgs>(Client_GetFeaturedMagnetCompleted);
            MagnetsStoreClient.Client.GetFeaturedMagnetAsync(InstancesSingleton.Instances.UsersViewModel.ConnectedUserSessionId);
        }

        void Client_GetFeaturedMagnetCompleted(object sender, GetFeaturedMagnetCompletedEventArgs e)
        {
            _featuredMagnet = new MagnetViewModel(e.Result);
            RaisePropertyChanged("FeaturedMagnet");
        }

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
